package com.maiji.cloud.controller;

import java.util.List;
import com.maiji.cloud.request.ReqMetaData;
import com.maiji.cloud.request.login.MaijiArticleReqData;
import com.maiji.cloud.response.*;
import com.maiji.cloud.response.login.MaijiArticleResData;
import com.maiji.cloud.response.login.UserInfoResData;
import com.maiji.cloud.service.*;
import com.maiji.cloud.utils.WXUtil;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jetty.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.maiji.cloud.entities.login.AppUser;
import com.maiji.cloud.entities.login.MaijiArticle;
import com.maiji.cloud.request.BaseDataReqDto;
import com.maiji.cloud.request.login.ArticleCommentReqData;
import com.maiji.cloud.response.login.ArticleCommentListResData;
import com.maiji.cloud.response.login.IsPraiseOrCollectResData;

@RestController
@RequestMapping("/article/")
public class ArticleController {
	private static final Logger logger = LoggerFactory.getLogger(ArticleController.class);

	@Autowired
	private ArticleService articleService;
	@Autowired
	private AppUserService appUserService;
    @Autowired
    private MaijiArticleService maijiArticleService;
    @Autowired
    private LabelMiddleMaijiArticleService labelMiddleMaijiArticleService;
    @Autowired
    private ArticleCommentService articleCommentService;

	/**
	 * 文章点赞
	 * 
	 * @param param 文章ID
	 * @param token
	 * @return
	 */
	@PostMapping("articlePraise")
	public BaseResDto articlePraise(@RequestBody BaseDataReqDto<String> param, @RequestHeader("maijiToken") String token) {
		logger.info("ArticleController.articlePraise param is {},token is {}", JSON.toJSONString(param), token);
		if (StringUtils.isBlank(param.getData()))
			return new BaseResDto(Status.PARAMETERERROR, "文章ID为空");
		return articleService.articlePraise(param, token);
	}

	/**
	 * 文章取消点赞
	 * 
	 * @param param
	 * @param token
	 * @return
	 */
	@PostMapping("cancelArticlePraise")
	public BaseResDto cancelArticlePraise(@RequestBody BaseDataReqDto<String> param, @RequestHeader("maijiToken") String token) {
		logger.info("ArticleController.cancelArticlePraise param is {},token is {}", JSON.toJSONString(param), token);
		if (StringUtils.isBlank(param.getData()))
			return new BaseResDto(Status.PARAMETERERROR, "文章ID为空");
		return articleService.cancelArticlePraise(param, token);
	}
	
	/**
	 * 根据文章ID获取点赞用户列表
	 * @param param
	 * @return
	 */
	@PostMapping("getArticlePraiseList")
	public BaseMetaResDto<List<UserInfoResData>> getArticlePraiseList(@RequestBody BaseDataReqDto<String> param){
		logger.info("ArticleController.getArticlePraiseList param is {}", JSON.toJSONString(param));
		
		return articleService.getArticlePraiseList(param);
	}
	
	/**
	 * 文章转发
	 * 
	 * @param param 文章ID
	 * @param token
	 * @return
	 */
	@PostMapping("articleTranspond")
	public BaseResDto articleTranspond(@RequestBody BaseDataReqDto<String> param, @RequestHeader("maijiToken") String token) {
		logger.info("ArticleController.articleTranspond param is {},token is {}", JSON.toJSONString(param), token);
		if (StringUtils.isBlank(param.getData()))
			return new BaseResDto(Status.PARAMETERERROR, "文章ID为空");
		return articleService.articleTranspond(param, token);
	}

	/**
	 * 文章评论/评论回复
	 * 
	 * @param param
	 * @param token
	 * @return
	 */
	@PostMapping("articleComment")
	public BaseResDto articleComment(@RequestBody BaseDataReqDto<ArticleCommentReqData> param, @RequestHeader("maijiToken") String token) {
		logger.info("ArticleController.articleComment param is {},token is {}", JSON.toJSONString(param), token);
		if (param.getData().getType() == null || (StringUtils.isBlank(param.getData().getArticleId())
				&& StringUtils.isBlank(param.getData().getCommentId())))
			return new BaseResDto(Status.PARAMETERERROR, "参数为空");
        String content = param.getData().getContent();
        if (! WXUtil.examineText(content)) return new BaseResDto(Status.NON_COMPLIANCE);
        return articleService.articleComment(param, token);
	}

	/**
	 * 获得评论信息(分页)
	 * @param param
	 * @return
	 */
	@PostMapping("getArticleCommentList")
	public BaseMetaResDto<List<ArticleCommentListResData>> getArticleCommentList(@RequestBody BaseDataReqDto<String> param) {
		logger.info("ArticleController.getArticleCommentList param is {}", JSON.toJSONString(param));
		if (StringUtils.isBlank(param.getData())) return new BaseMetaResDto<>(Status.PARAMETERERROR);
		return articleService.getArticleCommentList(param);
	}

	/**
	 * 根据评论ID获得评论回复列表（分页）
	 * 
	 * @param param
	 * @return
	 */
	@PostMapping("getArticleCommentReply")
	public BaseMetaResDto<List<ArticleCommentListResData>> getArticleCommentReply(@RequestBody BaseDataReqDto<String> param) {
		logger.info("ArticleController.getArticleCommentReply param is {}", JSON.toJSONString(param));
		if (StringUtils.isBlank(param.getData()))
			return new BaseMetaResDto<List<ArticleCommentListResData>>(Status.PARAMETERERROR);
		return articleService.getArticleCommentReply(param);
	}

	/**
	 * app用户操作删除评论
	 * 
	 * @param param
	 * @return
	 */
	@PostMapping("deleteArticleComment")
	public BaseResDto deleteArticleComment(@RequestBody BaseDataReqDto<String> param, @RequestHeader("maijiToken") String token) {
		logger.info("ArticleController.deleteArticleComment param is {},token is {}", JSON.toJSONString(param),token);
		if (StringUtils.isBlank(param.getData()))
			return new BaseResDto(Status.PARAMETERERROR);

		// 获得用户id
		AppUser appUser = appUserService.checkToken(token);
		if (appUser == null) {
			logger.info("ArticleController.deleteArticleComment,appUser is {}", JSON.toJSONString(appUser));
			return new BaseResDto(Status.PARAMETERERROR, "token 无效 ");
		}

		return articleService.deleteArticleComment(param,appUser.getUuId());
	}

    /**
     *  根据标签ID获取麦吉简介文章列表[]
     * @return
     */
    @PostMapping("findAllMaijiArticleByLabelId")
    public BaseDataResDto<List<MaijiArticleResData>> findAllMaijiArticleByLabelId (@RequestBody BaseDataReqDto<String> baseDataReqDto) {
        String labelId = baseDataReqDto.getData();
        if (StringUtil.isBlank(labelId)) return new BaseDataResDto(Status.PARAMETERERROR);
        return maijiArticleService.findAllMaijiArticleByLabelId(labelId);
    }

    /**
     *  删除或编辑麦吉简介文章
     * @return
     */
    @PostMapping("del_updMaijiArticleById")
    public BaseResDto del_updMaijiArticleById (@RequestBody BaseDataReqDto<MaijiArticle> baseDataReqDto) {
        MaijiArticle maijiArticle = baseDataReqDto.getData();
        if (StringUtil.isBlank(maijiArticle.getUuId())) return new BaseDataResDto(Status.PARAMETERERROR);
        return maijiArticleService.del_updMaijiArticleById(maijiArticle);
    }

    /**
     *  修改麦吉简介文章标签
     * @return
     */
    @PostMapping("updMaijiArticleLabel")
    public BaseResDto updMaijiArticleLabel (@RequestBody BaseDataReqDto<List<String>> baseDataReqDto) {
        List<String> labelIds = baseDataReqDto.getData();
        if (labelIds.size() == 0) return new BaseDataResDto(Status.PARAMETERERROR);
        return labelMiddleMaijiArticleService.updMaijiArticleLabel(labelIds.get(0), labelIds.get(1), labelIds.get(2), labelIds.get(3));
    }

    /**
     *  添加或编辑麦吉简介文章
     * @return
     */
    @PostMapping("add_updMaijiArticle")
    public BaseResDto add_updMaijiArticle (@RequestBody BaseDataReqDto<MaijiArticleReqData> baseDataReqDto) {
        try {
            MaijiArticleReqData data = baseDataReqDto.getData();
            return maijiArticleService.add_updMaijiArticle(data);
        } catch (Exception e) {
            e.printStackTrace();
            return new BaseResDto(Status.ERROR);
        }
    }

    /**
     *  获取麦吉简介文章评论列表
     * @return
     */
    @PostMapping("findAllArticleComments")
    public BaseMetaResDto<List<ArticleCommentListResData>> findAllArticleComments (@RequestBody BaseDataReqDto<String> baseDataReqDto,
                                                                                   @RequestHeader(name = "maijiToken", required = false) String token) {
        String articleId = baseDataReqDto.getData();
        ReqMetaData metaData = baseDataReqDto.getMetaData();
        return articleCommentService.findAllArticleComments(articleId, token, metaData);
    }

    /**
     *  删除屏蔽麦吉简介文章评论
     * @return
     */
    @PostMapping("delArticleComments")
    public BaseResDto delArticleComments (@RequestBody BaseDataReqDto<List<String>> baseDataReqDto) {
        List<String> commentIds = baseDataReqDto.getData();
        return articleCommentService.delArticleComments(commentIds);
    }

    /**
     * 根据文章id判断用户是否点赞
     * @param param
     * @param token
     * @return
     */
    @PostMapping("isPraiseOrCollect")
    public BaseDataResDto<IsPraiseOrCollectResData> isPraise(@RequestBody BaseDataReqDto<String> param,@RequestHeader("maijiToken") String token){
    	logger.info("ArticleController.isPraise param is {},token is {}", JSON.toJSONString(param),token);
    	// 获得用户id
    	AppUser appUser = appUserService.checkToken(token);
    	if (appUser == null) {
    		logger.info("ArticleController.isPraise,appUser is {}", JSON.toJSONString(appUser));
    		return new BaseDataResDto<IsPraiseOrCollectResData>(Status.PARAMETERERROR, "token 无效 ");
    		}
    	
    	if(StringUtils.isBlank(param.getData()))return new BaseDataResDto<IsPraiseOrCollectResData>(Status.PARAMETERERROR);
    	
    	return articleService.isPraise(param.getData(),appUser.getUuId());
    	
    }
    
    /**
	 * 文章收藏
	 * 
	 * @param param 文章ID
	 * @param token
	 * @return
	 */
	@PostMapping("articleCollect")
	public BaseResDto articleCollect(@RequestBody BaseDataReqDto<String> param, @RequestHeader("maijiToken") String token) {
		logger.info("ArticleController.articleCollect param is {},token is {}", JSON.toJSONString(param), token);
		if (StringUtils.isBlank(param.getData()))
			return new BaseResDto(Status.PARAMETERERROR, "文章ID为空");
		// 获得用户id
    	AppUser appUser = appUserService.checkToken(token);
    	if (appUser == null) {
    		logger.info("ArticleController.articleCollect,appUser is {}", JSON.toJSONString(appUser));
    		return new BaseDataResDto<Boolean>(Status.PARAMETERERROR, "token 无效 ");
    		}
		return articleService.articleCollect(param.getData(), appUser.getUuId());
	}

	/**
	 * 文章取消收藏
	 * 
	 * @param param
	 * @param token
	 * @return
	 */
	@PostMapping("cancelArticleCollect")
	public BaseResDto cancelArticleCollect(@RequestBody BaseDataReqDto<String> param, @RequestHeader("maijiToken") String token) {
		logger.info("ArticleController.cancelArticleCollect param is {},token is {}", JSON.toJSONString(param), token);
    	AppUser appUser = appUserService.checkToken(token);
    	if (appUser == null) {
            logger.info("ArticleController.cancelArticleCollect,appUser is {}", JSON.toJSONString(appUser));
            return new BaseDataResDto<Boolean>(Status.PARAMETERERROR, "token 无效 ");
        }
		return articleService.cancelArticleCollect(param.getData(), appUser.getUuId());
	}

	/**
	 * 文章收藏列表
	 * @param token
	 * @return
	 */
	@PostMapping("articleCollectList")
	public BaseDataResDto<List<MaijiArticle>> articleCollectList(@RequestBody BaseDataReqDto<String> param,@RequestHeader("maijiToken") String token){
    	AppUser appUser = appUserService.checkToken(token);
    	if (appUser == null) {
    		logger.info("ArticleController.articleCollectList,appUser is {}", JSON.toJSONString(appUser));
    		return new BaseDataResDto<List<MaijiArticle>>(Status.PARAMETERERROR, "token 无效 ");
    		}
		return articleService.articleCollectList(param.setData(appUser.getUuId()));
	}

	/**
	 * 获取文章详情
	 * @param baseDataReqDto
	 * @return
	 */
	@PostMapping("findMaijiArticleById")
	public BaseDataResDto<MaijiArticle> findMaijiArticleById (@RequestBody BaseDataReqDto<String> baseDataReqDto){
        String articleId = baseDataReqDto.getData();
        if (StringUtil.isBlank(articleId)) return new BaseDataResDto<>(Status.PARAMETERERROR);
        return articleService.findMaijiArticleById(articleId);
	}
	
	
}
